﻿$(function () {
    var Page_Size = 10;
    $("#btnFilterSos").live("click", btnFilterSosClick);
    $("#sosTable tbody tr").live("click", tbSosClick);//数据行点击事件
    $("#btnHandlerSOS").live("click", btnHandlerSOSClick);//援助事物处理完毕
    
    $("#btnReturn").live("click", function () {
        $("#sosDiv").show();
        $("#divSingleSOS").slideUp();
    });
    $("#btnFilterSos").click();

    function btnHandlerSOSClick()
    {
        var sosid = $(this).attr("data-sosid");
        $.getJSON("SosHandler/HandleSOS", { Sosid: sosid }, function (result) {
            if (result.Result.IsSuccess) {
                art.dialog.notice({
                    width: 120,
                    content: '处理成功',
                    icon: 'succeed',
                    time: 3
                });
                $("#btnFilterSos").click();
                $("#btnReturn").click();
            }
            else {
                art.dialog({
                    content: result.Result.ErrMessage
                });
            }
        });
    }

    function tbSosClick() {
        $("#sosDiv").hide();
        $("#divSingleSOS").slideDown(1000);
        var $container = $("#divSingleSOS");
        $container.empty();
        var sosid = $(this).attr("data-sosid");
        $.getJSON("SosHandler/GetSosById", { Sosid: sosid }, function (result) {
            var source = $("#SOSItem-template").html();
            var template = Handlebars.compile(source);
            var html = template(result);
            $container.append(html);
        });
    }

    function btnFilterSosClick() {
        var RolerId = $("#FilterRolers").val();
        var Type = $("#Type").val();
        var Category = $("#Category").val();
        GetflowLogPage({ Page: 1, PageSize: Page_Size, RolerId: RolerId, Type: Type, Category: Category });
    }

    function GetflowLogPage(f) {
        //分页控件   
        var count = GetDataCount(f);
        $("#pp").pagination(count, {
            link_to: "javascript:void(0)",
            num_edge_entries: 2, //边缘页数
            num_display_entries: 7, //主体页数
            callback: onSelectPageFun,
            items_per_page: Page_Size, //每页显示项
            prev_text: "&laquo;",
            next_text: "&raquo;"
        });

        //响应事件
        function onSelectPageFun(page) {
            f.page = page + 1;
            var url = "../SosHandler/GetSOSList";
            $.ajax({
                url: url,
                data: f,
                dataType: "json",
                beforeSend: function () {

                },
                type: "post",
                success: function (result) {
                    renderFun(result);//响应事件
                },
                async: true,
                complete: function () {
                }
            });
        }
        //END
    };

    //数据渲染
    function renderFun(result) {
        var $tbody = $("#sosTable tbody");
        $tbody.empty();
        var list = result;
        var source = $("#SOS-template").html();
        var template = Handlebars.compile(source);
        var html = template({ sosList: list });
        $tbody.append(html);
    }
    function GetDataCount(f) {
        var count = 0;
        $.ajax({
            url: "../SosHandler/GetSOSCount",
            data: f,
            success: function (res) {
                count = res;
            },
            async: false
        });
        return count;
    }
});